import json
import random
import re

from django import http
from django.shortcuts import render, redirect, reverse
from django.views import View
from django_redis import get_redis_connection

from apps.user.models import User
from libs.capt.captcha import captcha
from celery import Celery

app = Celery(broker='amqp://guest@localhost//')  # 消息队列的url


class Imgcode(View):
    def get(self, request):
        uuid = request.GET.get("code_id")
        _, text, img = captcha.generate_captcha()
        # 保存图片验证码
        redis_conn = get_redis_connection('code')
        redis_conn.setex('img_%s' % uuid, 60, text)
        return http.HttpResponse(img, content_type="image/png")


class Sms_code(View):
    def post(self, request):
        data = json.loads(request.body.decode('utf-8'))

        uuid = data.get("tkon_code")
        image_code = data.get("image_code")

        redis_conn = get_redis_connection('code')

        t_f = redis_conn.get("img_" + uuid + "/")
        moilbes = data.get("phone")
        phone = redis_conn.get("phone_code_%s" % moilbes)

        if not re.search(r"^1[3-9]\d{9}$", moilbes):
            return http.JsonResponse({'code': "208", 'errmsg': '手机号错误'})

        if phone:  # 防止手机号重复注册
            return http.JsonResponse({'code': "202", 'errmsg': '操作频繁'})

        if not all([uuid, image_code, moilbes]):
            return http.JsonResponse({'code': "201", 'errmsg': '缺少数据'})
        if not t_f:
            return http.JsonResponse({'code': "404", 'errmsg': '图片验证码已过期'})
        if t_f.decode().lower() != image_code.lower():
            return http.JsonResponse({'code': "200", 'errmsg': '验证码错误'})
        from apps.user.models import User
        if User.objects.filter(mobile=moilbes):
            return http.JsonResponse({'code': "8520", 'errmsg': '该手机号已被注册'})
        phone_code = random.randint(100000, 999999)
        print("正在发送短信验证码 ——————", phone_code)
        # 将信息发送
        app.send_task('send_sms.send', args=[moilbes, phone_code])

        # send_sms.send(moilbes, phone_code)

        redis_conn.setex("phone_code_%s" % moilbes, 60 * 5, phone_code)
        redis_conn.delete(t_f)
        return http.JsonResponse({'code': "0", 'errmsg': '手机验证码发送成功'})


class Forget_pwd_sms(View):
    def post(self, request):
        data = json.loads(request.body.decode('utf-8'))
        uuid = data.get("tkon_code")
        moilbes = data.get("phone")
        image_code = data.get("image_code")

        redis_conn = get_redis_connection('code')
        phone = redis_conn.get("phone_code_%s" % moilbes)
        t_f = redis_conn.get("img_" + uuid + "/")

        if not all([uuid, image_code, moilbes]):
            return http.JsonResponse({'code': "201", 'errmsg': '缺少数据'})
        if not re.search(r"^1[3-9]\d{9}$", moilbes):
            return http.JsonResponse({'code': "208", 'errmsg': '手机号错误'})
        if not User.objects.filter(mobile=moilbes):
            return http.JsonResponse({'code': "8520", 'errmsg': '该手机号未被注册'})
        if phone:  # 防止手机号重复发送短信
            return http.JsonResponse({'code': "202", 'errmsg': '操作频繁'})
        if not t_f:
            return http.JsonResponse({'code': "404", 'errmsg': '图片验证码已过期'})
        if t_f.decode().lower() != image_code.lower():
            return http.JsonResponse({'code': "200", 'errmsg': '验证码错误'})

        phone_code = random.randint(100000, 999999)
        print("正在发送短信验证码 ——————", phone_code)
        # 将信息发送
        # app.send_task('send_sms.send', args=[moilbes, phone_code])

        # send_sms.send(moilbes, phone_code)

        redis_conn.setex("phone_code_%s" % moilbes, 60 * 5, phone_code)
        redis_conn.delete(t_f)
        return http.JsonResponse({'code': "0", 'errmsg': '手机验证码发送成功'})
